package com.guoning.office_system.service.impl;

import com.guoning.office_system.mapper.FileMapper;
import com.guoning.office_system.model.File;
import com.guoning.office_system.model.User;
import com.guoning.office_system.model.WorkOrder;
import com.guoning.office_system.service.FileService;
import com.guoning.office_system.service.WorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {
    @Autowired
    FileMapper fileMapper;
    @Autowired
    WorkOrderService workOrderService;

    @Override
    public List<File> selectByOrderId(Long orderId) {
        return fileMapper.selectByOrderId(orderId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long insert(WorkOrder workOrder, List<File> list, User user) {
        workOrder.setOrderCode(String.valueOf(new Date().getTime() + workOrder.getUserId()));
        workOrder.setCreator(user.getUserName());
        Long workOrderId = workOrderService.insert(workOrder);
        if(list.size() > 0){
            for (int i=0; i<list.size(); i++){
                list.get(i).setOrderId(workOrderId);
                list.get(i).setUploader(user.getUserName());
            }
            fileMapper.insert(list);
        }
        return workOrderId;
    }

    @Override
    public int insertOne(File file, User user) {
        file.setUploader(user.getUserName());
        int i = fileMapper.insertOne(file);
        return i;
    }

    @Override
    public File selectByPrimaryKey(String id) {
        return fileMapper.selectByPrimaryKey(id);
    }
}